package edu.uoc.tdp.pac4.client.stats;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;
import javax.swing.text.DateFormatter;

import edu.uoc.tdp.pac4.client.ClientConnectionHelper;
import edu.uoc.tdp.pac4.common.util.LanguageUtils;

public class StatsStaffCoursesAndAttendance extends JPanel{
	private JFormattedTextField textField_1;
	private JTable table;
	private JPanel filterPanel = null;
	private JPanel resultsPanel = null;
	private JScrollPane resultsScroll = null;
	private JCheckBox checkBoxTots = null;
	private JComboBox courseCombo = null;
	private List<StatResultData> statsStudentResults = null;
	SimpleDateFormat dataFormatter = new SimpleDateFormat("dd/MM/yyyy");
	
	private JButton btnNewButton = null;
	
	//Connection to the services
	ClientConnectionHelper connectionHelper_ = null;
	
	public StatsStaffCoursesAndAttendance(ClientConnectionHelper connectionHelper) {
		setLayout(new BorderLayout(0, 0));

		connectionHelper_ = connectionHelper;

		// Filters
		filterPanel = new JPanel();
		filterPanel.setBorder(new TitledBorder(null, LanguageUtils.getMessage("client.statistics.filters"), TitledBorder.LEADING,TitledBorder.TOP, null, null));
		add(filterPanel, BorderLayout.NORTH);
		filterPanel.setLayout(new GridLayout(2, 4, 0, 0));

		// Primera Fila Filters
		JLabel lblNewLabel = new JLabel(LanguageUtils.getMessage("client.statistics.datainici"));
		filterPanel.add(lblNewLabel);

		DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
	    DateFormatter df = new DateFormatter(format);
	    textField_1 = new JFormattedTextField(df);
	    
		filterPanel.add(textField_1);
		filterPanel.add(new JLabel(" (dd/MM/yyyy)"));
		filterPanel.add(new JLabel());

		// Segona Fila Filters
		JLabel label = new JLabel(LanguageUtils.getMessage("client.statistics.curs.tipuscurs"));
		filterPanel.add(label);

		checkBoxTots = new JCheckBox(LanguageUtils.getMessage("client.statistics.curs.tots"));
		checkBoxTots.setSelected(true);
		checkBoxTots.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == checkBoxTots) {
					if(checkBoxTots.isSelected()){
						if(courseCombo.getSelectedItem() != null){
							courseCombo.setSelectedIndex(0);
						}
						checkBoxTots.setSelected(true);
					}else{
						courseCombo.setSelectedIndex(1);
					}
				}
				
			}
		});
		filterPanel.add(checkBoxTots);
		courseCombo = new JComboBox();
		courseCombo.setModel(StatsHelper.fillCourses(connectionHelper_));
		courseCombo.setPreferredSize(new Dimension(250,30));
		courseCombo.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == courseCombo) {
					if(courseCombo.getSelectedIndex()<0){
						checkBoxTots.setSelected(true);
					}else{
						checkBoxTots.setSelected(false);
					}
				}
				
			}
		});	
		filterPanel.add(new JLabel(LanguageUtils.getMessage("client.statistics.curs.concret")));
		filterPanel.add(courseCombo);

		// Results
		resultsPanel = new JPanel();
		resultsPanel.setLayout(new BorderLayout());

		resultsScroll = new JScrollPane(getResultsTable());
		resultsScroll.setViewportView(getResultsTable());
		resultsScroll.setColumnHeaderView(getResultsTable().getTableHeader());

		resultsPanel.add(resultsScroll);

		add(resultsPanel);

		JToolBar toolBar = new JToolBar();
		add(toolBar, BorderLayout.SOUTH);

		btnNewButton = new JButton(LanguageUtils.getMessage("client.statistics.cerca"));
		btnNewButton.setHorizontalAlignment(SwingConstants.RIGHT);
		JLabel info = new JLabel("  " + LanguageUtils.getMessage("client.stat.staff.2"));
		toolBar.add(btnNewButton);
		toolBar.add(info);
		btnNewButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == btnNewButton) {
					try {
						statsStudentResults = getStatisticResult();
						resultsScroll.setViewportView(getResultsTable());
					} catch (Exception e2) {
						JOptionPane.showMessageDialog(null, LanguageUtils
								.getMessage("client.error.message"),
								LanguageUtils.getMessage("client.error.title"),
								JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		});
	}

	private List<StatResultData> getStatisticResult(){
		List<StatResultData> list = null;
	
		Date date = null;
		if(textField_1 != null && textField_1.getText() !=null && !"".equals(textField_1.getText())){
			 try {
				date = dataFormatter.parse(textField_1.getText());
			} catch (ParseException e) {
			}
		}
		String codeCourse = null;
		if(!checkBoxTots.isSelected()){
			codeCourse = (String)courseCombo.getSelectedItem();
		}
		
		try{
			System.out.println("Sending Params: ");
			System.out.println("Date: " + date);
			System.out.println("Course: (if null==All)  " + codeCourse);
			list = connectionHelper_.getStatsManager().getStaffStat2(date, codeCourse);
		} catch (RemoteException e1) {
			// Remote Exception: Server error.
			JOptionPane.showMessageDialog(null, LanguageUtils
					.getMessage("client.error.message.remote"),
					LanguageUtils.getMessage("client.error.title"),
					JOptionPane.ERROR_MESSAGE);
		}
		
		return list;
	}
	private JTable getResultsTable() {
		JTable tableStudentResults = null;
		if (tableStudentResults == null) {
			tableStudentResults = new JTable();
			StatStaffTwoModel model = new StatStaffTwoModel(statsStudentResults);
			tableStudentResults.setModel(model);

		}
		tableStudentResults.setEnabled(false);
		return tableStudentResults;
	}
}
